import sys
from ui_MainWindow import Ui_MainWindow
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtMultimedia import *

class MyMainWindow(QMainWindow):
    def __init__(self,parent=None):
        super().__init__(parent)
        self.ui=Ui_MainWindow()
        self.ui.setupUi(self)
        self.player=QMediaPlayer(self)#创建视频播放器
        self.player.setNotifyInterval(1000)#信息更新日期
        self.player.setVideoOutput(self.ui.videoWidget)#视频显示组件
        self.ui.videoWidget.installEventFilter(self) #事件过滤器

        self.__duration=""#文件总长度
        self.__curPos=""#当前播放的位置"
        self.player.stateChanged.connect(self.do_stateChanged)
        self.player.positionChanged.connect(self.do_postionChanged)
        self.player.durationChanged.connect(self.do_durationChanged)

    def closeEvent(self, event):
        if(self.player.state()==QMediaPlayer.PlayingState):
            self.player.stop()

    def eventFilter(self,watched, event):     ##事件过滤器
       if (watched!=self.ui.videoWidget):
          return super().eventFilter(watched,event)

       #鼠标左键按下时，暂停或继续播放
       if event.type()==QEvent.MouseButtonPress:
          if event.button()==Qt.LeftButton:
             if self.player.state()==QMediaPlayer.PlayingState:
                self.player.pause()
             else:
                self.player.play()

       #全屏状态时，按ESC键退出全屏
       if event.type()==QEvent.KeyPress:
          if event.key() == Qt.Key_Escape:
             if self.ui.videoWidget.isFullScreen():
                self.ui.videoWidget.setFullScreen(False)

       return super().eventFilter(watched,event)
    @pyqtSlot()
    def on_btnOpen_clicked(self):
        curPath = QDir.currentPath()  # 获取系统当前目录
        title = "选择视频文件"
        filt = "视频文件(*.wmv *.avi);;所有文件(*.*)"
        filename, flt = QFileDialog.getOpenFileName(self, title, curPath, filt)
        if (filename == ""):
            return

        fileInfo = QFileInfo(filename)
        baseName = fileInfo.fileName()
        self.ui.LabCurMedia.setText(baseName)

        curPath = fileInfo.absolutePath()
        QDir.setCurrent(curPath)  # 重设当前目录

        media = QMediaContent(QUrl.fromLocalFile(filename))
        self.player.setMedia(media)  # 设置播放文件
        self.player.play()

    @pyqtSlot()
    def on_btnPlay_clicked(self):
        self.player.play()
    @pyqtSlot()
    def on_btnPause_clicked(self):
        self.player.pause()

    @pyqtSlot()
    def on_btnStop_clicked(self):
        self.player.stop()
    @pyqtSlot()
    def on_btnFullScreen_clicked(self):
        self.ui.videoWidget.setFullScreen(True)


    @pyqtSlot()
    def on_btnSound_clicked(self):
        mute=self.player.isMuted()
        self.player.setMuted(not mute)
        if (mute):
            self.ui.btnSound.setIcon(QIcon(":/icons/images/volumn.bmp"))
        else:
            self.ui.btnSound.setIcon(QIcon(":/icons/images/mute.bmp"))
    #调节音量
    @pyqtSlot(int)
    def on_sliderVolumn_valueChanged(self,value):
        self.player.setVolume(value)

    #控制文件的进度
    @pyqtSlot(int)
    def on_sliderPosition_valueChanged(self,value):
        self.player.setPosition(value)

    #播放器状态控制
    def do_stateChanged(self,state):
        isPlaying=(state==QMediaPlayer.PlayingState)
        self.ui.btnPlay.setEnabled(not isPlaying)
        self.ui.btnPause.setEnabled(isPlaying)
        self.ui.btnStop.setEnabled(isPlaying)
    #文件位置变化更新进度
    def do_postionChanged(self,postion):
        if (self.ui.sliderPosition.isSliderDown()):
            return
        self.ui.sliderPosition.setSliderPosition(postion)
        secs=postion/1000
        mins=secs/60
        secs=secs % 60
        self.__curPos="%d:%d" %(mins,secs)
        self.ui.LabRatio.setText(self.__curPos+"/"+self.__duration)

    def do_durationChanged(self,duration):
        self.ui.sliderPosition.setMaximum(duration)
        secs = duration / 1000
        mins = secs / 60
        secs = secs % 60
        self.__duration = "%d:%d" % (mins, secs)
        self.ui.LabRatio.setText(self.__curPos + "/" + self.__duration)

if __name__ == '__main__':
    app=QApplication(sys.argv)
    win=MyMainWindow()
    win.show()
    sys.exit(app.exec_())